package com.socodethis.spaceex.splashscreen;

import java.net.URL;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.socodethis.spaceex.Assets;
import com.socodethis.spaceex.ProgressBarClass;
import com.socodethis.spaceex.customGame;
import com.socodethis.spaceex.textureHolder;
import com.socodethis.spaceex.worldGeneratorClass;
import com.socodethis.spaceex.gamescreen.GameScreen;
import com.socodethis.spaceex.math.cPoint;
import com.socodethis.spaceex.utils.ScreenSize;

public class SplashScreen implements Screen{

	private customGame game;
	private long startTimer;
	private final long splashTime = 3000;
	private SpriteBatch spriteBatch;
	private Texture splash;
	private textureHolder texture = new textureHolder();
	private ProgressBarClass progressBar;
	
	public SplashScreen(customGame g){
		Assets.load();
		game = g;
		startTimer = System.currentTimeMillis();
		progressBar = new ProgressBarClass(new cPoint(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()), texture.returnProgressBar(),texture.returnPlayer_ld(true));
	}
	
	@Override
	public void render(float delta) {
		//draw
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        spriteBatch.begin();
        Assets.manager.get("data/font1.fnt", BitmapFont.class).draw(spriteBatch, "Created by So Code It Collective",
        		(Gdx.graphics.getWidth()/2)-(ScreenSize.getScaledWidth(splash.getWidth())+ScreenSize.getScaledWidth(splash.getWidth()/2)),
        		(Gdx.graphics.getHeight()/2)+(ScreenSize.getScaledHeight(splash.getHeight())/2)-ScreenSize.getScaledHeight(splash.getHeight()));
        spriteBatch.draw(splash,
        		(Gdx.graphics.getWidth()/2)-(ScreenSize.getScaledWidth(splash.getWidth())/2),
        		(Gdx.graphics.getHeight()/2),
        		ScreenSize.getScaledWidth(splash.getWidth()),
        		ScreenSize.getScaledHeight(splash.getHeight()));
        progressBar.getBar().draw(spriteBatch);
        progressBar.getCharacter().draw(spriteBatch);

        spriteBatch.end();
		
        progressBar.addProgress();
        
		//update
		//if splash timer runs out or screen is touched, exit splash
		if ((System.currentTimeMillis()>=startTimer+splashTime) | Gdx.input.justTouched()/*progressBar.getProgress()==100*/){
			//This loads a world with one region
			game.loadWorld(2);
			GameScreen gScreen = new GameScreen(game);
			game.holdGameScreen(gScreen);
			game.setScreen(gScreen);
		}
	}

	@Override
	public void resize(int width, int height) {
		
	}

	@Override
	public void show() {
		spriteBatch = new SpriteBatch();
		splash = Assets.manager.get("socodeit.png", Texture.class);
        //new BuildWorldTask().execute();
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void dispose() {
		
	}
	/*
	private class BuildWorldTask extends AsyncTask <Void, Void, Void> {
	     protected void onProgressUpdate(Void... progress) {
	    	 //progressBar.addFraction();
	     }

	     protected void onPostExecute(Void result) {
	    	 //There is nothing here
	     }

		@Override
		protected Void doInBackground(Void... arg0) {
			worldGeneratorClass mapGen;
			mapGen = new worldGeneratorClass(0);
			progressBar.setFraction(arg0[0]);
			for(int i = 0; i < arg0[0];i++){
				mapGen.addRegion();
				publishProgress();
			}
			game.setWorld(mapGen);
			// TODO Auto-generated method stub
			return null;
		}
	 }*/
}
